/**
 * 通用通知辅助工具
 * 提供简化的API接口，方便其他模块快速集成通知功能
 */

import { useProgressNotification } from '../hooks/useProgressNotification';
import { useUniversalNotification } from '../contexts/UniversalNotificationContext';
import { NotificationUtils, notificationTemplates } from './notificationTemplates';
import {
  NotificationType,
  NotificationStatus,
  ProgressInfo,
  ErrorInfo,
  StatisticsInfo,
  NotificationCallbacks,
} from '../types/notification';

// 简化的进度通知接口
export interface SimpleProgressOptions {
  title: string;
  description?: string;
  type?: NotificationType;
  category?: string;
  enableRetry?: boolean;
  callbacks?: NotificationCallbacks;
}

// 简化的通知选项
export interface SimpleNotificationOptions {
  title: string;
  description?: string;
  type?: NotificationType;
  status?: NotificationStatus;
  autoClose?: boolean;
  persistent?: boolean;
  icon?: React.ReactNode;
  actions?: Array<{
    label: string;
    action: () => void;
    type?: 'primary' | 'default' | 'danger';
  }>;
}

// 通用通知管理器类
export class UniversalNotificationManager {
  private progressHook: ReturnType<typeof useProgressNotification>;
  private notificationHook: ReturnType<typeof useUniversalNotification>;

  constructor(
    progressHook: ReturnType<typeof useProgressNotification>,
    notificationHook: ReturnType<typeof useUniversalNotification>
  ) {
    this.progressHook = progressHook;
    this.notificationHook = notificationHook;
  }

  // === 快速创建方法 ===

  // 创建文件上传通知
  createFileUpload(fileName: string, fileSize?: number, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createFileUploadNotification(fileName, fileSize);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建文件下载通知
  createFileDownload(fileName: string, fileSize?: number, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createFileDownloadNotification(fileName, fileSize);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建数据同步通知
  createDataSync(source: string, target: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createDataSyncNotification(source, target);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建数据处理通知
  createDataProcessing(dataType: string, operation: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createDataProcessingNotification(dataType, operation);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建备份通知
  createBackup(backupType: string, targetLocation: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createBackupNotification(backupType, targetLocation);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建导出通知
  createExport(dataType: string, format: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createExportNotification(dataType, format);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建导入通知
  createImport(dataType: string, source: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createImportNotification(dataType, source);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // 创建分析通知
  createAnalysis(analysisType: string, dataSource: string, callbacks?: NotificationCallbacks): string {
    const template = NotificationUtils.createAnalysisNotification(analysisType, dataSource);
    return this.progressHook.start({
      ...template,
      callbacks,
    });
  }

  // === 通用方法 ===

  // 创建进度通知
  createProgress(options: SimpleProgressOptions): string {
    return this.progressHook.start(options);
  }

  // 创建简单通知
  createNotification(options: SimpleNotificationOptions): string {
    const isSimpleNotification = ['info', 'warning', 'error', 'success'].includes(options.type || 'info');

    return this.notificationHook.add({
      type: options.type || 'info',
      status: options.status || (isSimpleNotification ? 'completed' : 'running'),
      title: options.title,
      description: options.description,
      icon: options.icon,
      actions: options.actions,
      config: {
        autoClose: options.autoClose ?? isSimpleNotification,
        persistent: options.persistent ?? !isSimpleNotification,
        displayStrategy: isSimpleNotification ? 'ant-notification' : 'dynamic-island',
        antNotificationDuration: isSimpleNotification ? 4 : 0,
      },
    });
  }

  // 更新进度
  updateProgress(
    id: string,
    progress: number | ProgressInfo,
    currentStage?: string,
    currentItem?: string,
    statistics?: StatisticsInfo
  ): void {
    this.progressHook.updateProgress(id, {
      progress,
      currentStage,
      currentItem,
      statistics,
    });
  }

  // 完成通知
  complete(id: string, success: boolean = true, summary?: string, statistics?: StatisticsInfo): void {
    this.progressHook.complete(id, {
      success,
      summary,
      statistics,
    });
  }

  // 失败通知
  fail(id: string, error: string | ErrorInfo): void {
    const errorInfo: ErrorInfo = typeof error === 'string' 
      ? { message: error }
      : error;
    
    this.progressHook.fail(id, errorInfo);
  }

  // 暂停通知
  pause(id: string, currentProgress?: number, currentStage?: string): void {
    this.progressHook.pause(id, currentProgress, currentStage);
  }

  // 恢复通知
  resume(id: string, currentProgress?: number, currentStage?: string): void {
    this.progressHook.resume(id, currentProgress, currentStage);
  }

  // 取消通知
  cancel(id: string, reason?: string): void {
    this.progressHook.cancel(id, reason);
  }

  // 移除通知
  remove(id: string): void {
    this.progressHook.remove(id);
  }

  // 设置状态
  setStatus(id: string, status: NotificationStatus, message?: string): void {
    this.progressHook.setStatus(id, status, message);
  }

  // === 批量操作 ===

  // 清理所有已完成的通知
  clearCompleted(): void {
    this.notificationHook.clearCompleted();
  }

  // 清理指定类型的通知
  clearByType(type: NotificationType): void {
    this.notificationHook.clearByType(type);
  }

  // 暂停所有通知
  pauseAll(): void {
    this.notificationHook.pauseAll();
  }

  // 恢复所有通知
  resumeAll(): void {
    this.notificationHook.resumeAll();
  }

  // === 查询方法 ===

  // 获取通知
  getNotification(id: string) {
    return this.notificationHook.get(id);
  }

  // 获取所有通知
  getAllNotifications() {
    return this.notificationHook.getAll();
  }

  // 按类型获取通知
  getNotificationsByType(type: NotificationType) {
    return this.notificationHook.getByType(type);
  }

  // 按状态获取通知
  getNotificationsByStatus(status: NotificationStatus) {
    return this.notificationHook.getByStatus(status);
  }
}

// Hook：提供简化的通知管理器
export const useNotificationManager = (): UniversalNotificationManager => {
  const progressHook = useProgressNotification();
  const notificationHook = useUniversalNotification();
  
  return new UniversalNotificationManager(progressHook, notificationHook);
};

// 一行代码集成的便捷函数
export const createQuickNotifications = () => {
  const manager = useNotificationManager();

  return {
    // 文件操作
    uploadFile: (fileName: string, fileSize?: number) => 
      manager.createFileUpload(fileName, fileSize),
    
    downloadFile: (fileName: string, fileSize?: number) => 
      manager.createFileDownload(fileName, fileSize),
    
    // 数据操作
    syncData: (source: string, target: string) => 
      manager.createDataSync(source, target),
    
    processData: (dataType: string, operation: string) => 
      manager.createDataProcessing(dataType, operation),
    
    analyzeData: (analysisType: string, dataSource: string) => 
      manager.createAnalysis(analysisType, dataSource),
    
    // 系统操作
    backup: (backupType: string, targetLocation: string) => 
      manager.createBackup(backupType, targetLocation),
    
    exportData: (dataType: string, format: string) => 
      manager.createExport(dataType, format),
    
    importData: (dataType: string, source: string) => 
      manager.createImport(dataType, source),
    
    // 通用操作
    progress: (title: string, description?: string) => 
      manager.createProgress({ title, description }),
    
    notify: (title: string, description?: string, type?: NotificationType) => 
      manager.createNotification({ title, description, type }),
    
    // 更新操作
    update: manager.updateProgress.bind(manager),
    complete: manager.complete.bind(manager),
    fail: manager.fail.bind(manager),
    pause: manager.pause.bind(manager),
    resume: manager.resume.bind(manager),
    cancel: manager.cancel.bind(manager),
    remove: manager.remove.bind(manager),
  };
};

// 导出常用的工具函数
export { NotificationUtils, notificationTemplates };
export * from '../types/notification';
export * from '../hooks/useProgressNotification';
export * from '../contexts/UniversalNotificationContext';
